package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.GroupBizService;
import com.neusoft.weekreport.component.entities.MsGroup;
import com.neusoft.weekreport.component.entities.MsGroupExample;
import com.neusoft.weekreport.component.entities.MsGroupRefUserExample;
import com.neusoft.weekreport.component.req.GetAddGroupInfoReq;
import com.neusoft.weekreport.component.req.GetGroupInfoByIdReq;
import com.neusoft.weekreport.component.req.GetGroupInfoReq;
import com.neusoft.weekreport.component.resp.GetAddGroupInfoResp;
import com.neusoft.weekreport.component.resp.GetGroupInfoByIdResp;
import com.neusoft.weekreport.component.resp.GetGroupInfoListResp;
import com.neusoft.weekreport.mapper.MsGroupMapper;
import com.neusoft.weekreport.mapper.MsGroupRefUserMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class GroupBizServiceImpl implements GroupBizService {

    @Resource
    MsGroupMapper msGroupMapper;
    @Resource
    MsGroupRefUserMapper msGroupRefUserMapper;

    //查询小组树信息
    @Override
    public GetGroupInfoListResp getGroupInfoList(GetGroupInfoReq req) throws Exception {
        GetGroupInfoListResp resp = new GetGroupInfoListResp();
        MsGroupExample msGroupExample = new MsGroupExample();
        MsGroupExample.Criteria criteria = msGroupExample.createCriteria();
        criteria.andEnableEqualTo(1);
        List<MsGroup> msGroups = msGroupMapper.selectByExample(msGroupExample);
        resp.setGroupInfo(msGroups);
        return resp;
    }

    //根据id查询小组信息
    @Override
    public GetGroupInfoByIdResp getGroupInfoById(GetGroupInfoByIdReq req) throws Exception {
        GetGroupInfoByIdResp resp = new GetGroupInfoByIdResp();
        MsGroup msGroup = msGroupMapper.selectByPrimaryKey(req.getId());
        resp.setGroupName(msGroup.getGroupName());
        resp.setDescription(msGroup.getDescription());
        resp.setId(req.getId());
        resp.setParentId(msGroup.getParentId());
        MsGroup msGroup1 = msGroupMapper.selectByPrimaryKey(msGroup.getParentId());
        if(msGroup.getParentId().equals("-1")){
            resp.setParentName("无");
        }else{
            resp.setParentName(msGroup1.getGroupName());
        }
        resp.setWeekFlag(msGroup.getWeekFlag());
        return resp;
    }

    //修改小组信息
    @Override
    public void EditGroupInfoById(GetAddGroupInfoReq req) throws Exception {
        MsGroup msGroup =new MsGroup();
        msGroup.setId(req.getId());
        msGroup.setGroupName(req.getGroupName());
        msGroup.setDescription(req.getDescription());
        msGroup.setUpdateDate(sysDateTime());
        msGroup.setWeekFlag(req.getWeekFlag());
        msGroupMapper.updateByPrimaryKeySelective(msGroup);
    }

    //添加组
    @Override
    public GetAddGroupInfoResp AddGroupInfoById(GetAddGroupInfoReq req) throws Exception {
        GetAddGroupInfoResp resp = new GetAddGroupInfoResp();
        String id = UUID.randomUUID().toString();
        MsGroup msGroup = new MsGroup();
        msGroup.setId(id);
        msGroup.setGroupName(req.getGroupName());
        msGroup.setDescription(req.getDescription());
        msGroup.setEnable(1);
        msGroup.setDeleteable(1);
        msGroup.setParentId(req.getParentId());
        msGroup.setCreateDate(sysDateTime());
        msGroup.setUpdateDate(sysDateTime());
        msGroup.setWeekFlag(req.getWeekFlag());
        msGroupMapper.insert(msGroup);
        resp.setId(id);
        return resp;
    }

    //删除组
    @Override
    @Transactional
    public void DeleteGroupInfoById(GetGroupInfoByIdReq req) throws Exception {
        //删除组
        msGroupMapper.deleteByPrimaryKey(req.getId());
        //删除组和用户关联表
        MsGroupRefUserExample msGroupRefUserExample = new MsGroupRefUserExample();
        MsGroupRefUserExample.Criteria criteria = msGroupRefUserExample.createCriteria();
        criteria.andGroupidEqualTo(req.getId());
        msGroupRefUserMapper.deleteByExample(msGroupRefUserExample);
    }

    private String sysDateTime(){
        Date date = new Date();
        DateFormat dataFormat = new SimpleDateFormat("yyy-MM-dd hh:mm:ss");
        String createDate = dataFormat.format(date);
        return createDate;
    }
}
